// src/bbs/post.rs
use sha2::{Sha256, Digest}; // 添加这行到文件顶部
use chrono::Utc;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize)] // 添加 Clone 支持
pub struct BbsPost {
    pub title: String,
    pub content: String,
    pub author: String,
    pub timestamp: i64, // 新增时间戳字段
    pub content_hash: Vec<u8>,  // 新增哈希字段
    pub url: String, // 新增字段（第7行）
}

impl BbsPost {
//    pub fn new(title: String, content: String, author: String) -> Self {
//        let content_hash = {
//            let mut hasher = Sha256::new();
//            hasher.update(&content);
//            hasher.finalize().to_vec()
//        };
    pub fn new(title: String, content: String, author: String) -> Self {
        let content_hash = {
            let mut hasher = Sha256::new();
            hasher.update(&content);
            hasher.finalize().to_vec()
        };
        
        Self {
            title,
            content,
            author,
            timestamp: Utc::now().timestamp(),
            content_hash,
	    url: String::new(), // 新增初始化（第22行）
        }
    }

    // 新增方法（在impl块末尾）
    pub fn with_url(mut self, url: String) -> Self {
        self.url = url;
        self
    }
}
